package com.estate.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.bean.Complaint;
import com.estate.bean.Repair;
import com.estate.mapper.ComplaintMapper;
import com.estate.service.feign.OwnerFeign;
import com.estate.service.feign.QuartersFeign;
import com.estate.service.service.ComplaintService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements ComplaintService {
    @Resource
    private QuartersFeign quartersFeign;
    @Resource
    private OwnerFeign ownerFeign;

    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Complaint> list = list();
        list.forEach(e -> {
            e.setQuarters(quartersFeign.find(e.getCommunityId()));
            e.setOwner(ownerFeign.find(e.getOwnerId()));
        });
        PageInfo<Complaint> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult saveComplaint(Complaint complaint) {
        complaint.setStatus(0);
        complaint.setIsDelete(0);
        log.info("新增complaint数据:{}", complaint);
        boolean b = save(complaint);
        if (!b) {
            return ResultTool.fail(501, "添加投诉成功");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult deleteComplaint(String complaintId) {
        log.info("删除的complaintId:{}", complaintId);
        boolean b = removeById(complaintId);
        if (!b) {
            return ResultTool.fail(502, "删除投诉成功");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult updateComplaint(Complaint complaint) {
        log.info("修改complaint数据:{}", complaint);
        boolean b = updateById(complaint);
        if (!b) {
            return ResultTool.fail(503, "修改投诉失败");
        }
        return ResultTool.success("success");
    }
}
